package
{
	import org.flixel.*;
	import org.flixel.plugin.photonstorm.*;
	
	import utilities.*;
	
	/**
	 * ...
	 * @author JAC
	 */
	public class Entity extends FlxSprite
	{
		public var state:uint;
		
		protected var _score:int;
		protected var _timer:FlxDelay;
		protected var _light:Light;
		protected var _textBoxList:TextBoxList;
		
		public function Entity(i:int = 0, j:int = 0, createLight:Boolean = true)
		{
			super();
			translateToTileByIndex(i, j);
			
			_score = 0;
			
			_timer = new FlxDelay(0);
			_timer.start();
			
			if (createLight)
			{
				_light = new Light(0, 0, this);
				_light.exists = false;
			}
			
			_textBoxList = new TextBoxList(this);
		}
		
		override public function destroy():void
		{
			if (_light != null)
			{
				Registry.lights.remove(_light);
				_light.destroy();
			}
			_textBoxList.destroy();
			
			super.destroy();
		}
		
		public function get score():int
		{
			return _score;
		}
		
		public function say(name:String):void
		{
			_textBoxList.play(name);
		}
		
		public function addText(name:String, phrase:String, width:int = 110, lifetime:Number = 1.5):void
		{
			_textBoxList.addText(name, phrase, width, lifetime);
		}
		
		public function changeText(name:String, phrase:String):void
		{
			_textBoxList.changeText(name, phrase);
		}
		
		public function translateToTileByPoint(point:FlxPoint):void
		{
			var tile:FlxPoint = Maze.getTileByPoint(point);
			translateToTileByIndex(tile.x, tile.y);
		}
		
		public function translateToTileByIndex(i:uint, j:uint):void
		{
			x = i * Maze.tileWidth + (width < Maze.tileWidth ? FlxU.abs(offset.x) : 0);
			y = j * Maze.tileHeight + (height < Maze.tileHeight ? FlxU.abs(offset.y) : 0);
		}
		
		public function translateToRandomTile():void
		{
			var randomTile:FlxPoint = Maze.getRandomPathTile();
			translateToTileByPoint(randomTile);
		}
		
		public function toggleLight():void
		{
			_light.exists = !_light.exists;
		}
		
		public function clearAnimations():void
		{
			_animations = new Array();
			_curAnim = null;
		}
		
		public function get outsideOfTheMaze():Boolean
		{
			return x < 0 || y < 0 || x > Maze.width - Maze.tileWidth || y > Maze.height - Maze.tileHeight;
		}
		
		public function get atExit():Boolean
		{
			return x < Maze.tileWidth - 5 || y < Maze.tileHeight - 5 || x > Maze.width - (Maze.tileWidth * 2) + 5 || y > Maze.height - (Maze.tileHeight * 2) + 5;
		}
	}
}